package com.example.rbacmanage.service.app.impl;

import com.example.rbacmanage.entity.app.Pv;
import com.example.rbacmanage.mapper.app.PvMapper;
import com.example.rbacmanage.service.app.PvService;
import com.example.rbacmanage.util.AddressUtils;
import com.example.rbacmanage.util.IpUtils;
import com.example.rbacmanage.util.ServletUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.Date;
import java.util.List;

@Service
public class PvServiceImpl implements PvService {
    @Autowired
    private PvMapper pvMapper;
    @Override
    public void save(Pv pv) {
        pv.setCreateTime(new Date());
        UserAgent userAgent = eu.bitwalker.useragentutils.UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        pv.setOs(os);
        pv.setBrowser(browser);
        pv.setIp(IpUtils.getIpAddr());
        pv.setIpLocation(AddressUtils.getRealAddressByIP(pv.getIp()));
        pvMapper.insert(pv);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            pvMapper.del(id);
        }
    }

    @Override
    public void update(Pv pv) {
        pv.setUpdateTime(new Date());
        pvMapper.update(pv);
    }

    @Override
    public List<Pv> getAll(Pv pv) {
        return pvMapper.list(pv);
    }

    @Override
    public PageInfo<Pv> getPage(Pv pv) {
        PageHelper.startPage(pv.getPage(),pv.getLimit());
        return new PageInfo<>(getAll(pv));
    }

    @Override
    public Pv getById(Long id) {
        return null;
    }

    @Override
    public Integer getCount(Pv pv) {
        return 0;
    }
}
